class Toolbar {
    constructor(table){
        this.table= table;
        this.newDocumentUrl = "toolbar/document";
        this.newDirectoryUrl = "toolbar/directory";
        this.init();
    }
    init(){
        this.bindSelector();
        this.bindEvents();
    }

    /**
     * 返回上一级目录
     */
    goBack() {
        if (window.currentDir.indexOf('/') != -1) {
            window.currentDir = window.currentDir.substring(0, window.currentDir.lastIndexOf("/"));
        } else {
            window.currentDir = "";
        };
        enterDir(window.currentDir);
    }

    /**
     * 返回跟目录
     */
    goHome () {
        console.log("返回根目录");
        window.currentDir = "";
        enterDir(window.currentDir);
    }

    /**
     * 使用模态框上传文件
     */
    goUpload() {
        this.uploadModal.on('hidden.bs.modal', () => {
            enterDir(window.currentDir);
        });
        console.log("goupload",this.uploadModal)
        this.uploadModal.modal()
    }

    createDocument () {
        console.log(this.newDocumentModal)
        let prefix = currentDir && currentDir + "/";
        this.newDocumentName.val(prefix);
        this.newDocumentModal.modal();
    }

    createDirectory () {
        console.log("createDirectory")
        let prefix = currentDir && currentDir + "/";
        this.newDirectoryName.val(prefix);
        this.newDirectoryModal.modal();
    }

    validateDoc(doc) {
        const re = new RegExp('^([^\\\\\\?\\/\\*\\|<>:\"]+/)*([^\\\\\\?\\/\\*\\|<>:\"])*\\.\\w+$');
        return re.test(doc);
    }

    validateDir(dir) {
        const re = new RegExp('^(?:[^\\\\\\?\\/\\*\\|<>:\"]+/)*(?:[^\\\\\\?\\/\\*\\|<>:\"])+$');
        return re.test(dir);
    }

    newDocFormSubmit () {
        let txt = this.newDocumentName.val();
        if (!this.validateDoc(txt)) {
            alert(txt + "不符合文件的命名规范");
            return false;
        }
        //名称校验
        //newDocumentName
        let ajaxOptions = {
            // 几个参数需要注意一下
            type: "get",// 方法类型
            dataType: "json",// 预期服务器返回的数据类型
            url: this.newDocumentUrl,// url
            data: this.newDocumentForm.serialize(),
            success: result => {
                log(result);// 打印服务端返回的数据(调试用)
                //关闭模态框
                this.newDocumentModal.modal('hide');
                if (result.code == 200) {
                    enterDir(currentDir);
                } else {
                    log(result.message);
                }
            },
            error:  () => {
                alert("异常！");
            }
        };
        $.ajax(ajaxOptions);
    }
    newDirFormSubmit () {

        var txt = this.newDirectoryName.val();
        if (!this.validateDir(txt)) {
            alert(txt + "不符合文件的命名规范");
            return false;
        }
        var ajaxOptions = {
            // 几个参数需要注意一下
            type: "get",// 方法类型
            dataType: "json",// 预期服务器返回的数据类型
            url: this.newDirectoryUrl,// url
            data: this.newDirectoryForm.serialize(),
            success: result=> {
                console.log(result);// 打印服务端返回的数据(调试用)
                this.newDirectoryModal.modal('hide');
                if (result.code == 200) {
                    enterDir(currentDir);
                } else {
                    log(result.message);
                }
            },
            error: () => {
                alert("异常！");
            }
        };
        log(ajaxOptions);
        $.ajax(ajaxOptions);
    }

    bindSelector () {
        //input
        this.newDocumentName = $('#new-document-input');
        this.newDocumentModal = $("#new-document-modal");
        this.newDirectoryName = $('#new-directory-input');
        this.newDirectoryModal = $('#new-directory-modal');
        //form
        this.newDocumentForm = $('#new-document-form');
        this.newDocumentFormSubmit = $('#new-document-submit');
        this.newDirectoryForm = $('#new-directory-form');
        this.newDirectoryFormSubmit = $('#new-directory-submit');
        //button
        this.goHomeBtn = $('#goHome');
        this.goBackBtn = $('#goBack');
        this.goUploadBtn = $('#goUpload');
        this.newDocumentBtn = $('#newDocument');
        this.newDirectoryBtn = $('#newDirectory');
        //upload
        this.uploadModal = $('#fileupload-modal');

    }

    bindEvents () {
        let self = this;
        this.goHomeBtn.click(function () {
            self.goHome();
        });

        this.goBackBtn.click(function () {
            self.goBack();
        });

        this.goUploadBtn.click(function () {
            self.goUpload();
        });
        this.newDocumentBtn.click(function () {
            self.createDocument();
        });
        this.newDirectoryBtn.click(function () {
            self.createDirectory();
        });
        this.newDocumentFormSubmit.click(function () {
            self.newDocFormSubmit();
        });
        //log('newDirFormSubmit', o.newDirectoryFormSubmit)
        this.newDirectoryFormSubmit.click(function () {
            //log('newDirFormSubmit')
            self.newDirFormSubmit();
        });
    }
}